<#--<#include "../../layout/base.ftl">-->
<#--<#macro header>-->
<script type="text/javascript">
    $(function(){
        var ygId = $("#ygIdforbmfw").val();
        var editTree = {
            allbm:$("#yg_allbm").tree({
                url:$.webPath() + '/sys/wzbm/get_treejson',
                checkbox:true,
                onExpand:function(node){
                    $(node.target).next("ul").find("li").each(function(){
                        var div = $(this).find(".tree-node");
                        var id = div.attr("node-id");
                        var bgNode = editTree.allbm.tree("find","'"+id+"'");
                        if(bgNode.attributes.bgCode){
                            div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                        }
                    });
                },
                onLoadSuccess:function(node,data){
                    if(!node){
                        node = $(this).tree("getRoot");
                        $(node.target).next("ul").find("li").each(function(){
                            var div = $(this).find(".tree-node");
                            var id = div.attr("node-id");
                            var bgNode = editTree.allbm.tree("find","'"+id+"'");
                            if(bgNode.attributes.bgCode){
                                div.attr("title","保供物资编码("+bgNode.attributes.bgCode+")");
                            }
                        });
                    }
                }
            }),
            editbm:$("#yg-editbm").tree({
                url:$.webPath() + '/organ/bmfw/get_treejsonbyyg?ygId='+ygId,
                checkbox:true,
                data:[{id: "0:0", text: "编码物资", attributes: {"level": "0", "children": "false"}, state: "open",children:[]}]
            }),
            appendToTree:function(node){
                var opt = editTree.editbm.tree("options");
                if(!opt.url){
                    opt.url = $.webPath() + '/sys/wzbm/get_treejson';
                }
                var parent = editTree.allbm.tree("getParent",node.target);
                if(!parent)
                    return;
                var editParent = editTree.editbm.tree("find","'"+parent.id+"'");
                if(!editParent){
                    editTree.appendToTree(parent);
                    editParent = editTree.editbm.tree("find","'"+parent.id+"'");
                }
                var editNode = editTree.editbm.tree("find","'"+node.id+"'");
                if(!editNode){
                    editTree.editbm.tree("append",{
                        parent:editParent.target,
                        data:[{
                            id:node.id,
                            text:node.text,
                            attributes:node.attributes,
                            state:node.state,
                            children:node.children
                        }]
                    });
                }
            },
            dropSelect:function(node){
                var parent = editTree.editbm.tree("getParent",node.target);
                if(!parent)
                    return;
                node = editTree.editbm.tree("find","'"+node.id+"'");
                if(node)
                    editTree.editbm.tree("remove",node.target);
            }
        };
        $("#ygbm-select").click(function(){
            var checked = editTree.allbm.tree("getChecked");
            for(var i=0;i<checked.length;i++){
                editTree.appendToTree(checked[i])
            }
        });
        $("#ygbm-cancel").click(function(){
            var checked = editTree.editbm.tree("getChecked");
            for(var i=0;i<checked.length;i++){
                editTree.dropSelect(checked[i])
            }
        });
        $("#ygbm-save").click(function(){
            var root = editTree.editbm.tree("getRoot");
            var data = editTree.editbm.tree("getChildren",root.target);
            ygbmfwwin.mask();
            $("#yg-bmfw-save").form("submit",{
                onSubmit: function (param) {
                    if(data.length == 0){
                        ygbmfwwin.mask("hide");
                        $.messager.alert("警告","未选中任何物资编码","warning");
                        return false;
                    }
                    for(var i=0;i<data.length;i++){
                        var children = editTree.editbm.tree("getChildren",data[i].target).length;
                        if(children != 0){
                            continue;
                        }
                        param["bmList["+i+"]"] = data[i].id;
                    }
                },
                success:function(data){
                    var obj = eval('(' + data + ')');
//                alert(obj.statusCode);
                    ygbmfwwin.mask("hide");
                    if(obj.statusCode == 200){
                        $.messager.show({title:'提示',msg:"保存成功",showType:'slide'});
                        ygbmfwwin.window("close");
                    } else {
                        $.messager.alert("info",data.message);
                    }
                }
            });
        });
    });
</script>
<#--</#macro>-->
<#--<#macro body>-->
<div id="yg-bmfw" style="width:100%;height:100%">

    <div style="width:40%;height:100%;float:left;border-right:1px #a6d9f8 solid;">
        <div id="yg-allbmpanel" class="easyui-panel" data-options="headerCls:'choosetool'"  fit="true" title="所有物资编码">
            <ul id="yg_allbm"></ul>
        </div>
    </div>
    <div style="width:19.7%;height:100%;float:left;">
        <a id="ygbm-select" href="javascript:void(0)" class="gongnengs" plain="true">选择</a>
        <a id="ygbm-cancel" href="javascript:void(0)" class="gongnengs" plain="true">取消</a>
        <a id="ygbm-save" href="javascript:void(0)" style="display: none"></a>
    </div>
    <div style="width:40%;height:100%;float:left;border-left:1px #a6d9f8 solid;">
        <div id="ygeditbmpanel" class="easyui-panel" data-options="headerCls:'choosetool'" fit="true" title="编辑物资编码">
            <ul id="yg-editbm"></ul>
        </div>
    </div>
    <form action="${base}/sys/yg/yg-savebmfw" id="yg-bmfw-save" method="post" style="display:none;"><input id="ygIdforbmfw" name="ygId" value="${(ygId)!}" type="hidden" /></form>
</div>
<#--</#macro>-->